package com.battcn.platform.controller.money;


import com.battcn.framework.common.annotation.BattcnLog;
import com.battcn.framework.common.exception.BattcnException;
import com.battcn.framework.mybatis.page.DataGrid;
import com.battcn.platform.controller.BaseController;
import com.battcn.platform.pojo.dto.money.ExpenditureDto;
import com.battcn.platform.pojo.dto.money.ExpenditureRecordDto;
import com.battcn.platform.pojo.dto.sys.ManagerDto;
import com.battcn.platform.pojo.message.ApiResult;
import com.battcn.platform.pojo.po.money.TbExpenditure;
import com.battcn.platform.pojo.po.money.TbExpenditureRecord;
import com.battcn.platform.pojo.po.sys.Manager;
import com.battcn.platform.pojo.po.sys.Office;
import com.battcn.platform.service.money.ExpenditureService;
import com.battcn.platform.service.sys.OfficeService;
import com.battcn.platform.util.SessionUtil;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;
import sun.security.krb5.internal.APOptions;

import java.util.Date;
import java.util.List;

import static com.battcn.framework.common.exception.BattcnException.notFound;

@Controller
@RequestMapping("/money/expenditure")//此处填写t_sys_menu的channel
@ApiIgnore//不加入swagger的扫描中去
public class ExpenditureController extends BaseController{
	
	@Autowired
	ExpenditureService expenditureService;
	@Autowired
	OfficeService officeService;

	@RequestMapping(value = "/list")
	public String list() throws BattcnException {
		return "money/expenditure/list";
	}

	@RequestMapping(value = "/query")
	@ResponseBody
	public PageInfo<ExpenditureDto> query(DataGrid grid, ExpenditureDto expenditure) {
		if(StringUtils.isEmpty(expenditure.getStatus())){
			expenditure.setStatus(null);
		}
		expenditure.setCreateBy(SessionUtil.getSession().getId());//查询自己的申请列表
		return this.expenditureService.findList(grid,expenditure);
	}

	@GetMapping(value = "/edit")
	public String edit(String id) {
		if (id != null) {
			request.setAttribute("dto", this.expenditureService.selectById(id).orElseThrow(() -> notFound("未找到记录")));
		}
		return "money/expenditure/edit";
	}
	@GetMapping(value = "/recallEdit")
	public String edit(ExpenditureRecordDto dto) {
		if (StringUtils.isNotEmpty(dto.getExpid())) {
			request.setAttribute("records", this.expenditureService.findRecordList(dto));
			request.setAttribute("dto", this.expenditureService.selectById(dto.getExpid()).orElseThrow(() -> notFound("未找到记录")));
		}
		return "money/expenditure/recallEdit";
	}


	@BattcnLog(module = "操作管理", methods = "保存操作", description = "添加经费申请信息")
	@PostMapping(value = "/save")
	@ResponseBody
	public ApiResult<String> save(TbExpenditure expenditure) {

		ManagerDto managerDto = SessionUtil.getSession();
		if(managerDto.getOffice() != null){
			Office office = this.officeService.selectById(managerDto.getOffice()).orElseThrow(() -> notFound("未找到记录"));
			if(StringUtils.isNotEmpty(office.getUser())){
				expenditure.setUser(office.getUser());	//部门负责人
				expenditure.setOfficeId(office.getId());
			}else{
				return ApiResult.getFailure("所在部门没有负责人，请联系管理员");
			}

		}

		//修改时，验证是否被审批
		if(StringUtils.isNotEmpty(expenditure.getId())){
			TbExpenditure t = this.expenditureService.selectById(expenditure.getId()).orElseThrow(() -> notFound("未找到记录"));
			if(StringUtils.isNotEmpty(t.getStatus()) && !t.getStatus().equals("0") && !t.getStatus().equals("5") && !t.getStatus().equals("7")){
				return ApiResult.getFailure("该记录已被审批，请刷新后撤回修改");
			}
			expenditure.setStatus("0");//未审核
			return ApiResult.getResponse(this.expenditureService.updateSelectiveById(expenditure));
		}
		expenditure.setStatus("0");//未审核
		return ApiResult.getResponse(this.expenditureService.insertSelective(expenditure));
	}

	@BattcnLog(module = "删除管理",methods = "移除操作",description = "删除申请经费")
	@PostMapping(value = "remove")
	@ResponseBody
	public ApiResult<String> remove(String[] ids){
		Lists.newArrayList(ids).forEach(this.expenditureService::deleteById);
		return ApiResult.SUCCESS;
	}

	@BattcnLog(module = "修改管理", methods = "保存操作", description = "修改申请经费状态")
	@PostMapping(value = "/updateStatus")
	@ResponseBody
	public ApiResult<String> updateStatus(TbExpenditureRecord record) {
		return expenditureService.updateStatus(record);
	}

	@BattcnLog(module = "修改管理", methods = "保存操作", description = "撤回修改")
	@PostMapping(value = "/recall")
	@ResponseBody
	public ApiResult<String> recall(ExpenditureRecordDto record) {
		if(StringUtils.isNotEmpty(record.getStatus())){
			if(record.getStatus().equals("1")){
				//如果拒绝撤回。流程继续，状态改为撤回之前的状态
				List<ExpenditureRecordDto> recordDtos = this.expenditureService.findRecordList(record);
				if(recordDtos !=  null && recordDtos.size() > 1){
					String status = recordDtos.get(recordDtos.size()-1).getStatus();
					record.setStatus(status);
				}
			}
		}else{
			return ApiResult.getFailure("数据错误");
		}
		expenditureService.updateStatus(record);
		return ApiResult.SUCCESS;
	}

}
